import Vue from 'vue'
import Vuex from "vuex"
Vue.use(Vuex)

const store = new Vuex.Store({
    state: {
        count: sessionStorage.getItem("count") || 1,
        list: JSON.parse(sessionStorage.getItem("list")) || [],
        info: JSON.parse(sessionStorage.getItem("info")) || {},
        messsage: JSON.parse(localStorage.getItem("msg")) || [],
        miuse:""
    },
    mutations: {
        
        //获取音乐
        miuse(state,a){
            state.miuse=a;
        },

        //todolist删除按钮
        dele(state,a){
            console.log(a.mes);
            var index = state.messsage.indexOf(a);
            console.log(index);
            if(index!=-1){
                state.messsage.splice(index,1);
                localStorage.setItem("msg",JSON.stringify(state.messsage));
            }

        },
        

        //todolist传值
        saveMessage(state, v) {
            state.messsage.push(v);
            localStorage.setItem("msg",JSON.stringify(state.messsage));
        },
        //todolist里isFnish改变
        changes(state,a){
            state.messsage.some((item, index, arr) => {
                if (item.id == a) {
                   item.isFnish = !item.isFnish;
                   console.log(a);
                    return true;
                } 
            })
            localStorage.setItem("msg",JSON.stringify(state.messsage));
        },  

        //把个人信息存起来
        saveInfo(state, a) {
            state.info = a;
            sessionStorage.setItem("info", JSON.stringify(state.info));
        },
        //添加到购物车
        addP(state, a) {
            // var index = state.list.indexOf(a);
            // console.log(index);
            // if(index!=-1){
            //   state.list[index].count++;
            // }else{
            //   state.list.push(a)
            // }

            var flag = false;
            state.list.some((item, index, arr) => {
                if (item.id == a.id) {
                    item.count++;
                    flag = true;
                    return true;
                }
            })
            if (!flag) {
                state.list.push(a)

            }
            sessionStorage.setItem("list", JSON.stringify(state.list))
        },

        pre(state, v) {
            state.list.some((item, index, arr) => {
                if (item.id == v.id) {
                    item.count++;
                    return true;
                }
            })
        },
        jia(state, v) {
            state.list.some((item, index, arr) => {
                if (item.id == v.id) {
                    if (item.count <= 1) {
                        arr.splice(index, 1);
                    } else {
                        item.count--;
                    }
                    return true;
                }
            })
            sessionStorage.setItem("list", JSON.stringify(state.list))
        },
        che(state, v) {
            state.list.some((item, index, arr) => {
                if (item.id == v.id) {
                    item.count = v.count;
                    return true;
                }
            })
            sessionStorage.setItem("list", JSON.stringify(state.list))
        },
        // page(state,a){
        //   console.log(a);
        //   state.list.push(a);
        //   sessionStorage.setItem("list",JSON.stringify(state.list))
        // },
        add(state, a) {
            state.count++;
            sessionStorage.setItem("count", state.count);
        },
        jian(state, a) {
            state.count--;
            sessionStorage.setItem("count", state.count);
        },
        change(state, a) {
            state.count = a;
            sessionStorage.setItem("count", state.count);
        }
    },
    getters: {
        alllCount(state) {
            var count = 0;
            state.list.forEach(item => {
                count += item.count;
            });
            return count;
        },
        getFnishi(state) {
            var Fnishi = state.messsage.filter(function (elem, index, arr) {
                return elem.isFnish == true;
                
            })
            return Fnishi;
        },
        getDoing(state) {
            var Doing = state.messsage.filter(function (elem, index, arr) {
                return elem.isFnish == false;
               
            })
            return Doing;
        }
    }
})
/* eslint-disable no-new */


export default store;